var imageSearch = {};

imageSearch.number = 5;
imageSearch.index = 0;

view.common = {
		
		renderInstall : function() {
			var params = {};
        	var html = Mustache.to_html(template.install.body, params);
        	
        	$(document.body).append(html);
        	
        	$("a.lnk-to-install").bind("click", function () {
        		gadgets.views.requestNavigateTo(gadgets.views.getSupportedViews()['preview']);
        	});
        	
    		gadgets.window.adjustHeight(400);
		},
		
		renderPreview : function() {
			var params = {};
			var html = Mustache.to_html(template.preview.body, params);
			
			$(document.body).append(html);
			
			gadgets.window.adjustHeight(200);
		},
		
		searchImages : function(query, target)
		{
			$.getJSON("https://ajax.googleapis.com/ajax/services/search/images?callback=?",
					  {
						key : "ABQIAAAAU1xChZ_GTaBF7V2ZioV2ihRaovBjn6IY_t4Xdre7jYGjVW19ohR__lzrtSzF2s5L5FjusW-XaGmufw",
						v : "1.0",
					    q: query,
					    rsz: imageSearch.number,
					    start: imageSearch.index,
					    imgsz: "medium",
					    hl : "de",
					    //as_sitesearch : "amazon.de",
					    as_filetype : "jpg"
					  },
					  function(data) {
						  view.common.renderImgList(data.responseData.results, target, query);
		     });
		},

    	renderImgList : function(data, prefix, query) {

        	var html = Mustache.to_html(template.canvas.images_list, {"images" : data});
        	
        	$("#img-" + prefix + "-result").html(html);
        	        	
        	$("div.search-list").toggle('slide', gadgets.window.adjustHeight);

        	$("a.img-search-item").unbind().bind("click", function () {
        		
        		$("#img-" + prefix + "-url").attr({value : this.rel});
        		$("#img-" + prefix + "-thumb").attr({src : this.rel});
        		
        	});
        	
        	$("img.preview").bind("error", function() {
        		view.common.showMessage(utils.getText("error.image_load"));
        	});
        	
        	$("a.img-search-next").bind('click', function() {
        		imageSearch.index += imageSearch.number;
        		view.common.searchImages(query, prefix);
        	});        	
        	
        	$("a.img-search-prev").bind('click', function() {
        		if (imageSearch.index > imageSearch.number) imageSearch.index -= imageSearch.number;
        		view.common.searchImages(query, prefix);
        	});        	
        },
        
        showMessage : function(message) {
        	$("#sys-message").html(message).slideDown().delay(2000).slideUp();
        },
        
        showErrorDialog : function(message, callback) {
    	   	$("#error-message").dialog("destroy");
     		$("#error-message p").html(message);
    		$("#error-message").dialog({
    		  modal: true,
    		  title: utils.getText("error.title"),
    		  buttons: {
    		 		OK: function() {
    		 			 $(this).dialog('close');
    		 			 
    		 			 if ($.isFunction(callback)) callback();
    				}
    		  }
    		});
        },
        
        showConfirmDialog : function(message, callback) {
        	$("#error-message").dialog("destroy");
        	$("#error-message p").html(message);
        	$("#error-message").dialog({
        		title : utils.getText("confirm.title"),
        		modal: true,
        		buttons: {
        			OK : function() { 
        				$(this).dialog('close');
        				$("#error-message").dialog("destroy");
        				if ($.isFunction(callback)) callback();
        			},
        			"Vergiss es": function() {
        				$(this).dialog('close');
        			}
        		}
        	});
        },

    	lockScreen : function() {
        	$('#loaderOverlay').show();
    	},
    	unlockScreen : function() {
        	$('#loaderOverlay').hide();
    	}
};